*************************************************************************************************
*																								*
*						Careers - Cohort-specificregressions, 2-year intervals					*
*						name file: regression_cohort_2yr.do										*
*						date: June 23, 2024														*
*																								*
*************************************************************************************************


*************************************************************************************************
*										Preamble												*
*************************************************************************************************



* ---> Michael For convenience, rename & label variables here
rename bipman_ev BD
rename bdsib_ev BDSIB
rename depr_ev DEPR
rename schizo_ev SCHIZO
rename anxiety_ev ANX // defined as "ever anxiety diagnosis"
rename earnings EARN
rename totalincome INC
rename disab DISAB
rename female FEMALE
rename birthcohort BIRTHCOHORT
rename year YEAR
rename momid MOMID
rename college COLLEGE // NOTE: This should be a variable equal to one for all people who have at least a college degree
rename highschool HS // NOTE: This should be a variable equal to one for all people who have at least a HS degree
gen MENTAL = DEPR == 1 | SCHIZO == 1 | BD == 1 | ANX == 1 // Note: added BD and anxiety
replace MENTAL = . if DEPR == . | SCHIZO == . | BD == . | ANX == .

label var MENTAL 	"MH condition"
label var BD 		"BD"
label var BDSIB 	"BD sibling"
label var DEPR 		"Depression"
label var SCHIZO 	"Schizophrenia"
label var ANX 		"Anxiety"

* "fix" or create some variables
gen LNEARN = ln(EARN)
replace LNEARN = 0 if LNEARN == .
gen NOEARN = EARN == 0
gen POSEARN = NOEARN == 0
gen LNINC = ln(INC) 
replace LNINC = 0 if LNINC == .
gen NOINC = INC == 0
gen POSINC = NOINC == 0
gen FEMALE_BD_POST = FEMALE * BD * POST

* ---> Cohort coefficients
global xvar = "BD DEPR SCHIZO ANX FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT"
local xvar_pose = "c.BD#c.NOEARN c.DEPR#c.NOEARN c.SCHIZO#c.NOEARN c.ANX#c.NOEARN c.FEMALE#c.NOEARN c.BD_POST#c.NOEARN"
local xvar_posi = "c.BD#c.NOINC c.DEPR#c.NOINC c.SCHIZO#c.NOINC c.ANX#c.NOINC c.FEMALE#c.NOINC c.BD_POST#c.NOINC"

global BDcoef1 = ""
global Mcoef1 = ""
global Dcoef1 = ""
global Scoef1 = ""
global Acoef1 = ""

global BDcoef2 = ""
global Mcoef2 = ""
global Dcoef2 = ""
global Scoef2 = ""
global Acoef2 = ""

gen BIRTHCOHORT2 = .

forvalues y = 1950(2)1952 {
local z = `y' + 1
replace BIRTHCOHORT2 = `y' if (BIRTHCOHORT == `y' | BIRTHCOHORT ==  `z')
gen BD_`y' = BD * (BIRTHCOHORT == `y' | BIRTHCOHORT == `z')
gen MENTAL_`y' = MENTAL * (BIRTHCOHORT == `y' | BIRTHCOHORT == `z')
label var  BD_`y' "`y'"
global BDcoef1 = "$BDcoef1 BD_`y'"
global Mcoef1 =  "$Mcoef1 MENTAL_`y'"
}
forvalues y = 1956(2)1978 {
local z = `y' + 1
replace BIRTHCOHORT2 = `y' if (BIRTHCOHORT == `y' | BIRTHCOHORT ==  `z')
gen BD_`y' = BD * (BIRTHCOHORT == `y' | BIRTHCOHORT == `z')
gen MENTAL_`y' = MENTAL * (BIRTHCOHORT == `y' | BIRTHCOHORT == `z')
label var  BD_`y' "`y'"
global BDcoef2 = "$BDcoef1 BD_`y'"
global Mcoef2 =  "$Mcoef1 MENTAL_`y'"
}

tempvar zero
gen `zero' = 0
label var `zero' "0"


*************************************************************************************************
*										Regressions												*
*************************************************************************************************

*---> DiD, for BD and Mental


keep if BIRTHCOHORT >= 1950


areg NOEARN $BDcoef1 `zero' $BDcoef2 BD DEPRANX SCHIZO  FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT2 i.YEAR i.BIRTHCOHORT2, a(MOMID) cluster(MOMID)
matrix A = e(b)
matrix beta = A[1,1..15]
matrix V = e(V)
matrix V = V[1..15,1..15]
mata st_matrix("sigma",sqrt(diagonal(st_matrix("V"))))
matrix sigma = sigma'
mat2txt,  matrix(beta) saving($out/beta_noearn_did.txt) replace
mat2txt,  matrix(V) saving($out/sigma_noearn_did.txt) replace
mat2txt,  matrix(sigma) saving($out/stderr_noearn_did.txt) replace

areg LNEARN $BDcoef1 `zero' $BDcoef2 BD DEPRANX SCHIZO  FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT2 i.YEAR i.BIRTHCOHORT2 if NOEARN == 0, a(MOMID) cluster(MOMID)
matrix A = e(b)
matrix beta = A[1,1..15]
matrix V = e(V)
matrix V = V[1..15,1..15]
mata st_matrix("sigma",sqrt(diagonal(st_matrix("V"))))
matrix sigma = sigma'
mat2txt,  matrix(beta) saving($out/beta_lnearn_did.txt) replace
mat2txt,  matrix(V) saving($out/sigma_lnearn_did.txt) replace
mat2txt,  matrix(sigma) saving($out/stderr_lnearn_did.txt) replace


*---> Triple Diff

areg NOEARN $BDcoef1 `zero' $BDcoef2 $Mcoef1 $Mcoef2 BD MENTAL  FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT2 i.YEAR i.BIRTHCOHORT2, a(MOMID) cluster(MOMID) 
matrix A = e(b)
matrix beta = A[1,1..15]
matrix V = e(V)
matrix V = V[1..15,1..15]
mata st_matrix("sigma",sqrt(diagonal(st_matrix("V"))))
matrix sigma = sigma'
mat2txt,  matrix(beta) saving($out/beta_noearn_triple.txt) replace
mat2txt,  matrix(V) saving($out/sigma_noearn_triple.txt) replace
mat2txt,  matrix(sigma) saving($out/stderr_noearn_triple.txt) replace

areg LNEARN $BDcoef1 `zero' $BDcoef2 $Mcoef1 $Mcoef2 BD  MENTAL FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT2 i.YEAR i.BIRTHCOHORT2 if NOEARN == 0, a(MOMID) cluster(MOMID) 
matrix A = e(b)
matrix beta = A[1,1..15]
matrix V = e(V)
matrix V = V[1..15,1..15]
mata st_matrix("sigma",sqrt(diagonal(st_matrix("V"))))
matrix sigma = sigma'
mat2txt,  matrix(beta) saving($out/beta_lnearn_triple.txt) replace
mat2txt,  matrix(V) saving($out/sigma_lnearn_triple.txt) replace
mat2txt,  matrix(sigma) saving($out/stderr_lnearn_triple.txt) replace

